程式碼如下:
[In]
kids=20
snack=100
soda=21
candy= 25
if candy>kids: #如果糖果大於小孩則打印
print('Kids will get diabetes')
if soda>kids: #如果蘇打水大於小孩則打印
print("There have 21 soda")
if kids != snack: #如果小孩人數不等於點心則打印
print('They save!')
[Out]
Kids will get diabetes
There have 21 soda
They save!
kids=10
snack=5
soda=8
candy= 3
if candy>kids: #如果糖果多於小孩就打印
print('Kids will get diabetes')
elif candy<kids: #反之
print("Kids are save")
else: #如果都沒有達到上述條件則打印
print('Kids dont know how to do that!')
if soda>kids:
print("There have 8 soda")
elif soda<kids:
print("The soda is not enough")
else:
print('Soda is good')
if kids != snack: #不等於
print('They save!')
if kids >= 10: #檢查左操作數的值是否大於或等於右操作數的值,如果是,則條件成立。
print("Too many kids")
[Out]
Kids are save
The soda is not enough
They save!
Too many kids
今日總結:
應該這樣解釋大家就對if else elif有著初步的認識了!